angular.module("module.passwordManage",[])
    .config(['$stateProvider',function ($stateProvider) {
        $stateProvider
            .state('home.passwordManage',{
                url:"/passwordManage",
                templateUrl:"passwordManage/passwordManage.html",
                controller:'PasswordManageController',
                controllerAs:'vm'
            })
    }])
    .controller('PasswordManageController',[function () {
        var vm=this;
        var type="";
        var oldPassword="";
        $('.gn_tabs a').click(function(e) {
            e.preventDefault();
//    这里调用了boostrap的tap(),实现了标签页的功能呢;
            var tabs=$(this).parent().parent().find(".active");
            tabs.removeClass("active");
            $(this).parent().addClass("active");
            type = e.target.hash.replace('#','');
            switch(type){
                case "moneyStatisticsPassword":
                    oldPassword=moneyStatisticsPassword;
                    break;
                case "goodsStatisticsPassword":
                    oldPassword=goodsStatisticsPassword;
                    break;
                case "rechargePassword":
                    oldPassword=rechargePassword;
                    break;
            }
            $("#oldPassword").val("");
            $("#newPassword").val("")
        });
        $('.gn_tabs .active a').trigger("click");
        $('#checkBtn').off().on("click",function () {
            console.log("oldPassword=",oldPassword);
            console.log('$("#oldPassword").val()=',$("#oldPassword").val());
            if ($("#oldPassword").val()==oldPassword){
                var password=$("#newPassword").val();
                $.post("/store/updatePassword",{type:type,password:password},function (res) {
                    try {
                        res = JSON.parse(res);
                    } catch (e) {
                        toast(e);
                    }
                    if (!res.succeed) return toast('修改密码失败，失败原因：' + res.message);
                    toast('修改密码成功,正在重新加载密码', 'success',function () {
                        $.get("/store/reloadPassword",{},function (res) {
                            try {
                                res = JSON.parse(res);
                            } catch (e) {
                                toast(e);
                            }
                            if (!res.succeed) return toast('重新加载密码失败，失败原因：' + res.message);
                        });
                        toast('加载密码成功,正在重新加载页面','success',function () {
                            location.reload();
                        })
                    });
                })
            }else {
                return toast("原密码错误，请重新输入")
            }
        })
    }]);